import originAxios from 'axios'
import axios from '../request'

export interface AssistantItemDB {
  create_time: string
  desc: string
  id: string
  logo?: string
  name: string
  update_time: string
  user_id: number
  user_name: string
  status: number
}

/**
 * 创建插件接口
 * POST请求
 * @returns Promise<any> 创建插件的结果
 */
export const createTool = async (data: any): Promise<any> => {
  // return await axios.post(`/api/v1/assistant/tool_list`, data)
  return await axios.post('/api/v1/tools/publish', data)
}

/**
 * 修改插件接口
 * PUT请求
 * @param toolId string 插件的ID
 * @returns Promise<any> 修改插件的结果
 */
export const updateTool = async (data: any): Promise<any> => {
  // return await axios.put(`/api/v1/assistant/tool_list`, data)
  return await axios.post('/api/v1/tools/publish', data)
}

/**
 * 保存插件接口
 * PUT请求
 * @param toolId string 插件的ID
 * @returns Promise<any> 修改插件的结果
 */
export const saveTool = async (data: any): Promise<any> => {
  // return await axios.put(`/api/v1/assistant/tool_list`, data)
  return await axios.post('/api/v1/tools/create', data)
}

/**
 * 插件历史版本接口
 * POST请求
 * @param toolId string 插件的ID
 * @returns Promise<any>
 */
export const getToolVersion = async (id: any): Promise<any> => {
  return await axios.post('/api/v1/tools/versions', { tool_id: id, full_field: true })
}

/**
 * 删除插件接口
 * DELETE请求
 * @returns Promise<any> 删除插件的结果
 */

export const deleteTool = async (id: string): Promise<any> => {
  return await axios({
    method: 'post',
    url: '/api/v1/tools/delete',
    data: {
      tool_id: id
    }
  })
}
/* 
获取删除总数 = 插件被引用
*/
export async function deleteToolCount(data) {
  return await axios.post('/api/v1/tools/used_count', data)
}

/**
 * 下载或解析tool schema的内容接口
 * POST请求
 * download_url string
 * file_content string
 * @returns Promise<any> 下载或解析tool schema的结果
 */
export const downloadToolSchema = async (data: { download_url: string } | { file_content: string }): Promise<any> => {
  return await axios.post('/api/v1/tools/schema', data)
}

/**
 * 插件测试接口
 * @returns
 */
export const testToolApi = async (data: {
  server_host: string
  extra: string
  auth_method: number
  auth_type: string
  api_key: string
  request_params: Object
}): Promise<any> => {
  return await originAxios({
    method: 'post',
    // url: '/api/v1/assistant/tool_test',
    url: '/api/v1/tools/test',
    data
  })
}

/**
 * 插件被使用时进行保存
 */
export function toolsSaveRecord(data) {
  return axios.post('/api/v1/tools/save/record', data)
}
